<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
<%Response.Charset="utf-8"%>
<!--#include file="conn.asp"-->

<%
    codeArr = array("11894","16852","21295","34315","55514","91328","134124","161367","169254","511153","571314","926515","4111213","13231111","28102828")
    yzmArr = array("a894","f852","2c95","343e","555d","9crr","c4b4","fc67","p9y4","5ae3","57cd","9p5e","ef9e","4abc","cwaa","rarr")

    ' if state = "1" then
        ' response.write "{""state"":""stop"",""msg"":""服务器维护中<br>请稍后再试""}"
        ' response.end
    ' end if
    
    ' 自己封装的函数
    private function jsonItem(key, value)
        if vartype(value) = vbstring then
            ' value为字符串，加引号
            jsonItem = """" & key & """:""" & value & """"
        else
            ' value不是字符串，不加引号
            jsonItem = """" & key & """:" & value
        end if
    end function

    private function jsonAddItem(data, key, value, isFirst)   ' true表示字符串，false表示数字
        if isFirst = true then
            jsonAddItem = data & jsonItem(key, value)
        else
            jsonAddItem = data & "," & jsonItem(key, value)
        end if
    end function

    private function booleanToJson(data)
        if data = True then
            booleanToJson = "true"
        else
            booleanToJson = "false"
        end if
    end function

    ' 标准日期到时间搓
    function toUnixTime(time)
        toUnixTime = DateDiff("s", "01/01/1970 08:00:00", time)
    end function

    ' 时间搓到标准日期
    function toNormalTime(unixtime)
        toNormalTime = cstr(DateAdd("s",unixtime, "01/01/1970 08:00:00"))
    end function

    action = request.form("action")


    select case action
    case "getVersion"
        data = "101"
        RANDOMIZE
        data = rnd()
        response.write data
        response.end
        
    case "login"
        ' 执行登录
        username = request.form("username")
        password = request.form("password")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from users, leveldatas where users.username = '" & username & "' and leveldatas.level = users.level"
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            response.write "{""state"":""error"", ""msg"":""用户不存在，请先注册""}"
        else
            ' 用户存在
            if rs("password") = password then
            
                ' 登录成功
                rs("logintime") = rs("logintime") + 1
                rs("lastlogindate") = toUnixTime(now())
                rs.update

                session("ID") = rs("ID")
                session("username") = username
                session.timeout = 60 * 24   ' 登录后不退出最多保持在线1天

                
                data = jsonAddItem("{", "state", "ok", true)
                data = jsonAddItem(data, "ID", rs("ID"), false)
                data = jsonAddItem(data, "username", rs("username"), false)
                data = jsonAddItem(data, "level", rs("users.level"), false)
                data = jsonAddItem(data, "exp", rs("exp"), false)
                data = jsonAddItem(data, "expMax", rs("expMax"), false)
                data = jsonAddItem(data, "coin", rs("coin"), false)
                data = jsonAddItem(data, "diamond", rs("diamond"), false)
                data = jsonAddItem(data, "musicbg", booleanToJson(rs("musicbg")), false)
                data = jsonAddItem(data, "musicclick", booleanToJson(rs("musicclick")), false)
                data = data & ",""goods"":["
                ' 获取游戏物品数据
                set rs = server.createobject("adodb.recordset")
                sql = "select * from goods"
                rs.open sql,conn,1,1

                if rs.bof and rs.eof then
                    ' 获取物品数据失败
                else
                    i = 0
                    do while not rs.eof
                        if i = 0 then
                            i = 1
                            data = jsonAddItem(data & "{", "ID", rs("ID"), true)
                        else
                            data = jsonAddItem(data & ",{", "ID", rs("ID"), true)
                        end if
                        data = jsonAddItem(data, "name", rs("gname"), false)
                        data = jsonAddItem(data, "type", rs("type"), false)
                        data = jsonAddItem(data, "buyprice", rs("buyprice"), false)
                        data = jsonAddItem(data, "saleprice", rs("saleprice"), false)
                        data = jsonAddItem(data, "exp", rs("exp"), false)
                        data = jsonAddItem(data, "ripetime", rs("ripetime"), false)
                        data = jsonAddItem(data, "diamondripeprice", rs("diamondripeprice"), false)
                        data = data & "}"
                        rs.movenext
                    loop
                end if
                data = data & "]"

                ' 获取土地数据
                set rs = server.createobject("adodb.recordset")
                sql = "select * from lands where LID = " & session("ID") & " order by index"
                rs.open sql,conn,1,1

                data = data & ",""lands"":["

                if rs.bof and rs.eof then
                    
                else
                    i = 0
                    do while not rs.eof
                        if i = 0 then
                            i = 1
                            data = jsonAddItem(data & "{", "ID", rs("ID"), true)
                        else
                            data = jsonAddItem(data & ",{", "ID", rs("ID"), true)
                        end if
                        data = jsonAddItem(data, "index", rs("index"), false)
                        data = jsonAddItem(data, "type", rs("type"), false)
                        data = jsonAddItem(data, "GID", rs("GID"), false)
                        data = jsonAddItem(data, "planttime", rs("planttime"), false)
                        
                        data = data & "}"
                        rs.movenext
                    loop
                end if

                ' 获取仓库信息
                set rs = server.createobject("adodb.recordset")
                sql = "select * from house where UID = " & session("ID")
                rs.open sql,conn,1,1

                data = data & "],""house"":["

                if rs.bof and rs.eof then
                    ' 读取失败
                else
                    i = 0
                    do while not rs.eof
                        if i = 0 then
                            i = 1
                            data = jsonAddItem(data & "{", "ID", rs("HID"), true)
                        else
                            data = jsonAddItem(data & ",{", "ID", rs("HID"), true)
                        end if
                        data = jsonAddItem(data, "number", rs("number"), false)
                        data = data & "}"
                        rs.movenext
                    loop
                end if

                data = data & "]"

                ' 获取公告信息
                set rs = server.createobject("adodb.recordset")
                sql = "select publictext from publica"
                rs.open sql,conn,1,1

                data = jsonAddItem(data, "public", rs("publictext"), false)
                data = data + "}"
                response.write data
            else
                ' 密码错误
                response.write "{""state"":""error"", ""msg"":""密码错误""}"
            end if
        end if
        
    case "register"
        ' 注册游戏
        username = request.form("username")
        password = request.form("password")
        yzm = request.form("yzm")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from users where username = '" & username & "'"
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            ' 用户名不存在，可以注册
            if yzm = yzmArr(session("codeIndex")) then
                rs.addnew
                rs("username") = username
                rs("password") = password
                rs("registerdate") = toUnixTime(now())
                rs.update

                ' 查询ID
                set rs = server.createobject("adodb.recordset")
                sql = "select ID from users where username = '" & username & "'"
                rs.open sql,conn,1,1

                if rs.bof and rs.eof then
                    response.write "{""state"":""error"", ""msg"":""用户不存在""}"
                else
                    ID = rs("ID")

                    ' 添加土地数据
                    set rs = server.createobject("adodb.recordset")
                    sql = "select * from lands"
                    rs.open sql,conn,1,3
                    for i = 0 to 23
                        rs.addnew
                        rs("LID") = ID
                        rs("index") = i
                        if i <= 3 then
                            rs("type") = "lock"
                        end if
                        rs.update
                    next

                    ' 添加仓库数据
                    set rs = server.createobject("adodb.recordset")
                    sql = "select * from house"
                    rs.open sql,conn,1,3
                    for i = 1 to 30
                        rs.addnew
                        rs("UID") = ID
                        rs("HID") = i
                        rs.update
                    next

                    ' 注册全部成功完成
                    response.write "{""state"":""ok""}"
                end if
            else
                ' 验证码错误
                session("codeIndex") = -1
                response.write "{""state"":""error"", ""msg"":""验证码错误，注册失败！""}"
            end if
        else
            ' 用户名重复，注册失败
            session("codeIndex") = -1
            response.write "{""state"":""error"", ""msg"":""用户名重复，注册失败！""}"
        end if

    case "getCode"
        ' 获取验证码
        RANDOMIZE
        index = int(rnd() * 16)
        session("codeIndex") = index
        response.write "{""state"":""ok"",""code"":" & codeArr(index) & "}"
        response.end

    case "getLandsState"
        ' 获取土地状态
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select lands.planttime, lands.index from (users inner join lands on users.ID = lands.LID) inner join goods on lands.GID = goods.ID where users.ID = " & ID & " order by lands.index"
        rs.open sql,conn,1,1

        if rs.bof and rs.eof then
            data = "{""state"":""ok"",""msg"":""未找到土地"""
        else
            data = "{""state"":""ok"""
            do while not rs.eof
                if rs("planttime") <= toUnixTime(now()) then
                    data = jsonAddItem(data, cstr(rs("index")), 0, false)
                else
                    data = jsonAddItem(data, cstr(rs("index")), rs("planttime") - toUnixTime(now()), false)
                end if
                rs.movenext
            loop
        end if

        set rs = server.createobject("adodb.recordset")
        sql = "select mine from users where ID = " & ID
        rs.open sql,conn,1,1

        if rs("mine") <= toUnixTime(now()) then
            data = jsonAddItem(data, "mine", 0, false)
        else
            data = jsonAddItem(data, "mine", rs("mine") - toUnixTime(now()), false)
        end if
        data = data + "}"
        response.write data

    case "reapCrop"
        ' 请求收割庄稼
        ID = request.form("ID")
        landID = request.form("landID")

        set rs = server.createobject("adodb.recordset")
        sql = "SELECT * FROM goods, ((users INNER JOIN lands ON users.ID = lands.LID) INNER JOIN house ON house.UID = users.ID) inner join leveldatas on users.level = leveldatas.level WHERE lands.GID + 1 = house.HID and lands.ID = " & landID & " and users.ID = " & ID & " and goods.ID = lands.GID"
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""error"", ""msg"":""未找到相关数据""}"
        else
            if rs("lands.type") = "lock" then
                data = "{""state"":""error"",""msg"":""该土地未扩建""}"
            else
                if rs("GID") = -1 then
                    data = "{""state"":""error"",""msg"":""该土地没有种植作物""}"
                else
                    if rs("planttime") > toUnixTime(now()) then
                        data = "{""state"":""error"",""msg"":""庄稼还没有成熟哦""}"
                    else
                        if rs("lands.type") = "plain" then
                            addNumber = 10
                            goodExp = rs("goods.exp")
                        end if
                        if rs("lands.type") = "red" then
                            addNumber = 12
                            goodExp = round(rs("goods.exp") * 1.1)
                        end if
                        if rs("lands.type") = "black" then
                            addNumber = 14
                            goodExp = round(rs("goods.exp") * 1.2)
                        end if
                        if rs("lands.type") = "blue" then
                            addNumber = 16
                            goodExp = round(rs("goods.exp") * 1.3)
                        end if
                        if rs("lands.type") = "purple" then
                            addNumber = 18
                            goodExp = round(rs("goods.exp") * 1.4)
                        end if
                        if rs("lands.type") = "gold" then
                            addNumber = 20
                            goodExp = round(rs("goods.exp") * 1.5)
                        end if

                        rs("number") = rs("number") + addNumber
                        rs("GID") = -1
                        
                        
                        nowExp = rs("users.exp") + goodExp
                        if nowExp >= rs("expMax") then
                            ' 升级了
                            if rs("users.level") <= 99 then
                                nowExp = nowExp - rs("expMax")
                                rs("users.level") = rs("users.level") + 1
                                rs("users.exp") = nowExp
                            end if
                            level = rs("users.level")
                            rs.update

                            set rs = server.createobject("adodb.recordset")
                            sql = "select expMax from leveldatas where level = " & level
                            rs.open sql,conn,1,1
                            
                            data = jsonAddItem("{", "state", "ok", true)
                            data = jsonAddItem(data, "level", level, false)
                            data = jsonAddItem(data, "addNumber", addNumber, false)
                            data = jsonAddItem(data, "addExp", goodExp, false)
                            data = jsonAddItem(data, "expMax", rs("expMax"), false)
                            data = jsonAddItem(data, "isUpgrade", "true", false)
                            data = data + "}"
                        else 
                            ' 没有升级
                            rs("users.exp") = nowExp
                            rs.update
                            
                            data = jsonAddItem("{", "state", "ok", true)
                            data = jsonAddItem(data, "addNumber", addNumber, false)
                            data = jsonAddItem(data, "addExp", goodExp, false)
                            data = jsonAddItem(data, "isUpgrade", "false", false)
                            data = data + "}"
                        end if
                    end if
                end if
            end if
        end if
        response.write data

    case "plantCrop"
        ' 请求种植庄稼
        landID = request.form("landID")
        goodID = cint(request.form("goodID"))
        ID = request.form("ID")
        set rs = server.createobject("adodb.recordset")
        sql = "select house.number, lands.type, lands.GID, lands.planttime, goods.ripetime from ((users inner join house on users.ID = house.UID) inner join lands on users.ID = lands.LID) ,goods where goods.ID = " & goodID & " and users.ID = " & ID & " and lands.ID = " & landID & " and house.HID = " & goodID
        rs.open sql,conn,1,3
        
        if rs.bof and rs.eof then
            data = "{""state"":""error"", ""msg"":""未找到相关数据""}"
        else
            if rs("type") = "lock" then
                data = "{""state"":""error"", ""msg"":""该土地未解锁""}"
            else
                if rs("GID") = -1 then
                    if rs("number") > 0 then
                        rs("number") = rs("number") - 1
                        rs("GID") = goodID
                        rs("planttime") = toUnixTime(now()) + rs("ripetime") * 60
                        rs.update
                        data = "{""state"":""ok"", ""seednumber"":" & rs("number") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""没有该种子了""}"
                    end if
                else
                    data = "{""state"":""error"", ""msg"":""土地不是空闲状态""}"
                end if
            end if
        end if
        response.write data
    
    case "saleGood"
        ' 请求卖出物品
        ID = request.form("ID")
        goodID = request.form("goodID")
        saleNumber = request.form("saleNumber")

        set rs = server.createobject("adodb.recordset")
        sql = "select users.coin, house.number, goods.saleprice from (users inner join house on users.ID = house.UID) inner join goods on house.HID = goods.ID where goods.ID = " & goodID & " and users.ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""error"", ""msg"":""未找到请求数据""}"
        else
            saleNumber = csng(saleNumber)
            if saleNumber > rs("number") then
                saleNumber = rs("number")
            end if

            if saleNumber = 0 then
                data = "{""state"":""error"", ""msg"":""该物品已卖出""}"
            else
                addCoin = saleNumber * rs("saleprice")
                rs("coin") = rs("coin") + addCoin
                rs("number") = rs("number") - saleNumber
                rs.update

                data = "{"
                data = jsonAddItem(data, "state", "ok", true)
                data = jsonAddItem(data, "remainNumber", rs("number"), false)
                data = jsonAddItem(data, "addCoin", addCoin, false)
                data = data + "}"
            end if
        end if
        response.write data
    
    case "buySeed"
        ' 请求购买种子
        seedID = request.form("seedID")
        buySeedNumber = csng(request.form("buySeedNumber"))
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select users.coin, house.number, goods.buyprice from (users inner join house on users.ID = house.UID) inner join goods on house.HID = goods.ID where users.ID = " & ID & " and goods.ID = " & seedID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            ' 未找到该种子
            data = jsonAddItem("{", "state", "error", true)
            data = jsonAddItem(data, "msg", "该用户请求的种子未找到", false)
            data = data + "}"
        else
            if rs("coin") >= buySeedNumber * rs("buyprice") then
                rs("number") = rs("number") + buySeedNumber
                addCoin = - buySeedNumber * rs("buyprice")
                rs("coin") = rs("coin") + addCoin
                rs.update
                data = jsonAddItem("{", "state", "ok", true)
                data = jsonAddItem(data, "addCoin", addCoin, false)
                data = data + "}"
            else
                data = "{""state"":""error"",""msg"":""金币不足""}"
            end if
        end if
        response.write data
        
    case "setMusic"
        ' 设置音效状态
        ID = request.form("ID")
        musicbg = request.form("musicbg")
        musicclick = request.form("musicclick")
        
        set rs = server.createobject("adodb.recordset")
        sql = "select musicbg, musicclick from users where ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""error"",""msg"",""用户不存在""}"
        else
            rs("musicbg") = musicbg
            rs("musicclick") = musicclick
            rs.update
            data = "{""state"":""ok""}"
        end if
        response.write data

    case "updatePwd"
        ' 修改密码
        ID = request.form("ID")
        oldPwd = request.form("oldPwd")
        newPwd = request.form("newPwd")

        set rs = server.createobject("adodb.recordset")
        sql = "select password from users where ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""error"",""msg"":""用户不存在""}"
        else
            if rs("password") = oldPwd then
                rs("password") = newPwd
                rs.update
                data = "{""state"":""ok""}"
            else
                data = "{""state"":""error"",""msg"":""旧密码不正确""}"
            end if
        end if
        response.write data

    case "extendLand"
        ' 扩建土地
        ID = cint(request.form("ID"))

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.plainLevel,landdatas.plainCoin,users.coin,users.level,lands.index,lands.type from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'lock'"
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""finish"",""msg"":""土地已全部扩建完""}"
        else
            if rs("plainLevel") <= rs("level") then
                ' 等级满足条件
                if rs("coin") >= rs("plainCoin") then
                    ' 金币满足条件，可以扩建
                    landIndex = rs("index")
                    rs("coin") = rs("coin") - rs("plainCoin")
                    plainCoin = rs("plainCoin")
                    rs("type") = "plain"
                    rs.update

                    if landIndex < 23 then
                        set rs = server.createobject("adodb.recordset")
                        sql = "select lands.type from users inner join lands on users.ID = lands.LID where lands.index = " & (landIndex + 1) & " and users.ID = " & ID
                        rs.open sql,conn,1,3
                        rs("type") = "lock"
                        rs.update
                    end if
            
                    data = "{""state"":""ok"",""addCoin"":" & -plainCoin & "}"
                else
                    data = "{""state"":""error"",""msg"":""金币不足""}"
                end if
            else
                data = "{""state"":""error"",""msg"":""等级不足""}"
            end if

            
        end if
        response.write data
        
    case "getUpgradeAfterDatas"
        ' 获取升级后的相关数据
        ID = cint(request.form("ID"))

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.plainLevel,landdatas.plainCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'lock'"
        rs.open sql,conn,1,1

        if rs.bof and rs.eof then
            ' 土地已全部扩建完毕
            data = jsonAddItem("{", "state", "ok", true)
            data = jsonAddItem(data, "plainLevel", -1, false)
            data = jsonAddItem(data, "plainCoin", -1, false)
        else
            data = jsonAddItem("{", "state", "ok", true)
            data = jsonAddItem(data, "plainLevel", rs("plainLevel"), false)
            data = jsonAddItem(data, "plainCoin", rs("plainCoin"), false)
        end if

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.redLevel,landdatas.redCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'plain' order by landdatas.plainLevel"
        rs.open sql,conn,1,1

        if rs.bof and rs.eof then
            ' 红土地已全部升级完毕
            data = jsonAddItem(data, "redLevel", -1, false)
            data = jsonAddItem(data, "redCoin", -1, false)
        else
            data = jsonAddItem(data, "redLevel", rs("redLevel"), false)
            data = jsonAddItem(data, "redCoin", rs("redCoin"), false)
        end if

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.blackLevel,landdatas.blackCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'red' order by landdatas.redLevel"
        rs.open sql,conn,1,1

        if rs.bof and  rs.eof then
            ' 黑土地已全部升级完毕
            data = jsonAddItem(data, "blackLevel", -1, false)
            data = jsonAddItem(data, "blackCoin", -1, false)
        else
            data = jsonAddItem(data, "blackLevel", rs("blackLevel"), false)
            data = jsonAddItem(data, "blackCoin", rs("blackCoin"), false)
        end if

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.blueLevel,landdatas.blueCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'black' order by landdatas.blackLevel"
        rs.open sql,conn,1,1

        if rs.bof and  rs.eof then
            ' 蓝土地已全部升级完毕
            data = jsonAddItem(data, "blueLevel", -1, false)
            data = jsonAddItem(data, "blueCoin", -1, false)
        else
            data = jsonAddItem(data, "blueLevel", rs("blueLevel"), false)
            data = jsonAddItem(data, "blueCoin", rs("blueCoin"), false)
        end if

        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.purpleLevel,landdatas.purpleCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'blue' order by landdatas.blueLevel"
        rs.open sql,conn,1,1

        if rs.bof and  rs.eof then
            ' 紫土地已全部升级完毕
            data = jsonAddItem(data, "purpleLevel", -1, false)
            data = jsonAddItem(data, "purpleCoin", -1, false)
        else
            data = jsonAddItem(data, "purpleLevel", rs("purpleLevel"), false)
            data = jsonAddItem(data, "purpleCoin", rs("purpleCoin"), false)
        end if
        
        set rs = server.createobject("adodb.recordset")
        sql = "select landdatas.goldLevel,landdatas.goldCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on landdatas.index = lands.index where users.ID = " & ID & " and lands.type = 'purple' order by landdatas.purpleLevel"
        rs.open sql,conn,1,1

        if rs.bof and rs.eof then
            ' 金土地已全部升级完毕
            data = jsonAddItem(data, "goldLevel", -1, false)
            data = jsonAddItem(data, "goldCoin", -1, false)
        else
            data = jsonAddItem(data, "goldLevel", rs("goldLevel"), false)
            data = jsonAddItem(data, "goldCoin", rs("goldCoin"), false)
        end if
        data = data + "}"

        response.write data

    case "upgradeLand"
        ' 升级土地
        ID = cint(request.form("ID"))
        landIndex = cint(request.form("landIndex"))
        mtype = request.form("type")

        ' 升级红土地
        if mtype = "0" then
            set rs = server.createobject("adodb.recordset")
            sql = "select lands.type, users.coin, landdatas.redCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on lands.index = landdatas.index where users.ID = " & ID & " and lands.index = " & landIndex
            rs.open sql,conn,1,3

            if rs.bof and rs.eof then
                data = "{""state"":""error"",""msg"":""土地不存在""}"
            else
                if rs("type") = "plain" then
                    if rs("coin") >= rs("redCoin") then
                        rs("type") = "red"
                        rs("coin") = rs("coin") - rs("redCoin")
                        rs.update
                        data = "{""state"":""ok"",""addCoin"":" & -rs("redCoin") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""金币不足""}"
                    end if
                else
                    data = "{""state"":""error"",""msg"":""升级红土地的条件必须为普通土地""}"
                end if
            end if
        end if

        if mtype = "1" then
            ' 升级黑土地
            set rs = server.createobject("adodb.recordset")
            sql = "select lands.type, users.coin, landdatas.blackCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on lands.index = landdatas.index where users.ID = " & ID & " and lands.index = " & landIndex
            rs.open sql,conn,1,3

            if rs.bof and rs.eof then
                data = "{""state"":""error"",""msg"":""土地不存在""}"
            else
                if rs("type") = "red" then
                    if rs("coin") >= rs("blackCoin") then
                        rs("type") = "black"
                        rs("coin") = rs("coin") - rs("blackCoin")
                        rs.update
                        data = "{""state"":""ok"",""addCoin"":" & -rs("blackCoin") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""金币不足""}"
                    end if
                else
                    data = "{""state"":""error"",""msg"":""升级黑土地的条件必须为红土地""}"
                end if
            end if
        end if
        
        if mtype = "2" then
            ' 升级蓝土地
            set rs = server.createobject("adodb.recordset")
            sql = "select lands.type, users.coin, landdatas.blueCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on lands.index = landdatas.index where users.ID = " & ID & " and lands.index = " & landIndex
            rs.open sql,conn,1,3

            if rs.bof and rs.eof then
                data = "{""state"":""error"",""msg"":""土地不存在""}"
            else
                if rs("type") = "black" then
                    if rs("coin") >= rs("blueCoin") then
                        rs("type") = "blue"
                        rs("coin") = rs("coin") - rs("blueCoin")
                        rs.update
                        data = "{""state"":""ok"",""addCoin"":" & -rs("blueCoin") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""金币不足""}"
                    end if
                else
                    data = "{""state"":""error"",""msg"":""升级蓝土地的条件必须为黑土地""}"
                end if
            end if
        end if
        
        if mtype = "3" then
            ' 升级紫土地
            set rs = server.createobject("adodb.recordset")
            sql = "select lands.type, users.coin, landdatas.purpleCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on lands.index = landdatas.index where users.ID = " & ID & " and lands.index = " & landIndex
            rs.open sql,conn,1,3

            if rs.bof and rs.eof then
                data = "{""state"":""error"",""msg"":""土地不存在""}"
            else
                if rs("type") = "blue" then
                    if rs("coin") >= rs("purpleCoin") then
                        rs("type") = "purple"
                        rs("coin") = rs("coin") - rs("purpleCoin")
                        rs.update
                        data = "{""state"":""ok"",""addCoin"":" & -rs("purpleCoin") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""金币不足""}"
                    end if
                else
                    data = "{""state"":""error"",""msg"":""升级紫土地的条件必须为蓝土地""}"
                end if
            end if
        end if
        
        if mtype = "4" then
            ' 升级金土地
            set rs = server.createobject("adodb.recordset")
            sql = "select lands.type, users.coin, landdatas.goldCoin from (users inner join lands on users.ID = lands.LID) inner join landdatas on lands.index = landdatas.index where users.ID = " & ID & " and lands.index = " & landIndex
            rs.open sql,conn,1,3

            if rs.bof and rs.eof then
                data = "{""state"":""error"",""msg"":""土地不存在""}"
            else
                if rs("type") = "purple" then
                    if rs("coin") >= rs("goldCoin") then
                        rs("type") = "gold"
                        rs("coin") = rs("coin") - rs("goldCoin")
                        rs.update
                        data = "{""state"":""ok"",""addCoin"":" & -rs("goldCoin") & "}"
                    else
                        data = "{""state"":""error"",""msg"":""金币不足""}"
                    end if
                else
                    data = "{""state"":""error"",""msg"":""升级金土地的条件必须为紫土地""}"
                end if
            end if
        end if

        response.write data

    case "mine"
        ' 收矿
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from users where ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then
            data = "{""state"":""error"",""msg"":""用户不存在""}"
        else
            if rs("mine") >= toUnixTime(now()) then
                data = "{""state"":""error"",""msg"":""矿场冷却中""}"
            else
                rs("mine") = toUnixTime(now()) + 3600
                rs.update
                data = "{""state"":""ok"", ""mine"":" & rs("mine")
                data = jsonAddItem(data, "mineCoin", rs("level") * 2, false)
                data = jsonAddItem(data, "mine", 3600, false)
                data = data + "}"
            end if
        end if

        response.write data

    case "advise"
        ' 建议反馈
        ID = request.form("ID")
        username = request.form("username")
        advise = request.form("advise")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from advises"
        rs.open sql,conn,1,3

        rs.addnew
        rs("UID") = ID
        rs("username") = username
        rs("content") = advise
        rs("datetime") = now()
        rs.update

        data = "{""state"":""ok""}"

        response.write data

    case "diamondReap"
        ' 钻石成熟
        ID = request.form("ID")
        landID = request.form("landID")

        set rs = server.createobject("adodb.recordset")
        sql = "select diamond,planttime,diamondripeprice from (users inner join lands on users.ID = lands.LID) inner join goods on lands.GID = goods.ID where lands.ID = " & landID & " and users.ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then 
            data = "{""state"":""error"",""msg"":""没有找到该作物""}"
        else
            if rs("planttime") > toUnixTime(now()) then
                ' 没有成熟
                if rs("diamond") >= rs("diamondripeprice") then
                    rs("diamond") = rs("diamond") - rs("diamondripeprice")
                    rs("planttime") = toUnixTime(now())
                    rs.update
                    data = "{""state"":""ok"",""diamond"":" & rs("diamond") &"}"
                else
                    data = "{""state"":""error"",""msg"":""钻石不足""}"
                end if
            else
                data = "{""state"":""error"",""msg"":""作物已成熟""}"
            end if
            
        end if

        response.write data

    case "signin"
        ' 每日签到
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from users inner join leveldatas on users.level = leveldatas.level where ID = " & ID
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then 
            data = "{""state"":""error"",""msg"":""没有找到该用户""}"
        else
            dat = toUnixTime(now())

            tian = int(dat / 3600 / 24)
            if tian = rs("signindate") then
                data = "{""state"":""done"",""msg"":""今天已经签到过了，明天再来哟""}"
            else
                if tian mod 4 = 0 then
                    ' 签到领金币
                    if rs("users.level") < 10 then
                        coin = 50
                    end if
                    if rs("users.level") >= 10 and rs("users.level") < 20 then
                        coin = 100
                    end if
                    if rs("users.level") >= 20 and rs("users.level") < 30 then
                        coin = 200
                    end if
                    if rs("users.level") >= 30 and rs("users.level") < 50  then
                        coin = 500
                    end if
                    if rs("users.level") >= 50 and rs("users.level") < 70 then
                        coin = 1000
                    end if
                    if rs("users.level") >= 70 then
                        coin = 2000
                    end if
                    rs("coin") = rs("coin") + coin
                    rs("signindate") = tian
                    rs.update
                    data = "{""state"":""ok"",""type"":""coin"",""num"":" & coin & "}"
                end if
                if tian mod 4 = 1 then
                    ' 签到领经验
                    if rs("users.level") < 10 then
                        expm = 6
                    end if
                    if rs("users.level") >= 10 and rs("users.level") < 20 then
                        expm = 20
                    end if
                    if rs("users.level") >= 20 and rs("users.level") < 30 then
                        expm = 50
                    end if
                    if rs("users.level") >= 30 and rs("users.level") < 50  then
                        expm = 150
                    end if
                    if rs("users.level") >= 50 and rs("users.level") < 70 then
                        expm = 500
                    end if
                    if rs("users.level") >= 70 then
                        expm = 2000
                    end if
                    rs("signindate") = tian

                    nowExp = rs("exp") + expm
                    if nowExp >= rs("expMax") then
                        ' 升级了
                        if rs("users.level") <= 99 then
                            nowExp = nowExp - rs("expMax")
                            rs("users.level") = rs("users.level") + 1
                            rs("exp") = nowExp
                        end if
                        level = rs("users.level")
                        rs.update

                        set rs = server.createobject("adodb.recordset")
                        sql = "select * from leveldatas inner join users on users.level = leveldatas.level where users.level = " & level
                        rs.open sql,conn,1,1
                        
                        data = jsonAddItem("{", "state", "ok", true)
                        data = jsonAddItem(data, "level", level, false)
                        data = jsonAddItem(data, "num", expm, false)
                        data = jsonAddItem(data, "expMax", rs("expMax"), false)
                        data = jsonAddItem(data, "isUpgrade", "true", false)
                        data = jsonAddItem(data, "type", "exp", false)
                        data = data + "}"
                    else 
                        ' 没有升级
                        rs("exp") = nowExp
                        rs.update
                        
                        data = jsonAddItem("{", "state", "ok", true)
                        data = jsonAddItem(data, "num", expm, false)
                        data = jsonAddItem(data, "isUpgrade", "false", false)
                        data = jsonAddItem(data, "expMax", rs("expMax"), false)
                        data = jsonAddItem(data, "type", "exp", false)
                        data = data + "}"
                    end if
                end if
                if tian mod 4 = 2 then
                    ' 签到领钻石
                    if rs("users.level") < 10 then
                        diamond = 2
                    end if
                    if rs("users.level") >= 10 and rs("users.level") < 20 then
                        diamond = 3
                    end if
                    if rs("users.level") >= 20 and rs("users.level") < 30 then
                        diamond = 4
                    end if
                    if rs("users.level") >= 30 and rs("users.level") < 50  then
                        diamond = 5
                    end if
                    if rs("users.level") >= 50 and rs("users.level") < 70 then
                        diamond = 6
                    end if
                    if rs("users.level") >= 70 then
                        diamond = 7
                    end if
                    rs("signindate") = tian
                    rs("diamond") = rs("diamond") + diamond
                    rs.update
                    data = jsonAddItem("{", "state", "ok", true)
                    data = jsonAddItem(data, "type", "diamond", false)
                    data = jsonAddItem(data, "num", diamond, false)
                    data = data + "}"
                end if
                if tian mod 4 = 3 then
                    ' 签到领种子
                    RANDOMIZE
                    seedID1 = int(rnd() * 15) * 2 + 1
                    RANDOMIZE
                    seedID2 = int(rnd() * 15) * 2 + 1

                    set rs = server.createobject("adodb.recordset")
                    sql = "select number from house where UID = " & ID & " and HID = " & seedID1
                    rs.open sql,conn,1,3

                    if bof and eof then
                        data = "{""state"":""error"",""msg"":""没有找到第一个种子""}"
                    else
                        rs("number") = rs("number") + 1
                        rs.update

                        set rs = server.createobject("adodb.recordset")
                        sql = "select number from house where UID = " & ID & " and HID = " & seedID2
                        rs.open sql,conn,1,3

                        if rs.bof and rs.eof then
                            data = "{""state"":""error"",""msg"":""没有找到第二个种子""}"
                        else
                            rs("number") = rs("number") + 1
                            rs.update

                            set rs = server.createobject("adodb.recordset")
                            sql = "select signindate from users where ID = " & ID
                            rs.open sql,conn,1,3

                            if rs.bof and rs.eof then
                                data = "{""state"":""error"",""msg"":""没有找到请求的用户""}"
                            else
                                rs("signindate") = tian
                                rs.update
                                data = "{""state"":""ok"",""type"":""seed"",""seedID1"":" & seedID1 & ",""seedID2"":" & seedID2 & "}"
                            end if
                        end if
                    end if
                end if
            end if
        end if

        response.write data
    
    case "checkReply"
        ' 检查石否有新回复
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from advises where UID = " & ID & " and isLook = False"
        rs.open sql,conn,1,1

        if rs.bof and rs.eof then 
            data = "{""state"":""error"",""msg"":""没有新回复""}"
        else
            if rs("reply") = "" then
                data = "{""state"":""error"",""msg"":""没有新回复""}"
            else
                data = "{""state"":""ok"",""reply"":""" & rs("reply") & """, ""isLook"":" & booleanToJson(rs("isLook")) & "}"
            end if
        end if

        response.write data
    case "deleteReply"
        ' 删除回复
        ID = request.form("ID")

        set rs = server.createobject("adodb.recordset")
        sql = "select * from advises where UID = " & ID & " and isLook = False"
        rs.open sql,conn,1,3

        if rs.bof and rs.eof then 
            data = "{""state"":""error"",""msg"":""没有这条消息""}"
        else
            rs("isLook") = true
            rs.update
            data = "{""state"":""ok""}"
        end if

        response.write data
    
    
    end select


    rs.close
    set rs = nothing
%>
